//
//  GYHomeTypeCollectView.m
//  GYDemo
//
//  Created by Mrzhang on 2018/6/29.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYHomeTypeCollectView.h"
#import "Head.h"
@interface GYHomeTypeCollectView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView *mCollection;

@end
@implementation GYHomeTypeCollectView
@synthesize mCollection;

-(void)layoutSubviews
{
    [super layoutSubviews];
    [mCollection reloadData];
    
}
-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self)
    {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];

        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];//设置其布局方向
        flowLayout.sectionInset = UIEdgeInsetsMake(0.0f, 0.0f, 6, 0.0f);//设置其边界
        flowLayout.minimumInteritemSpacing = WFCGFloatX(3);
        flowLayout.minimumLineSpacing = 6;
        //其布局很有意思，当你的cell设置大小后，一行多少个cell，由cell的宽度决定
        
        mCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(WFCGFloatX(15), 0, frame.size.width -WFCGFloatX(30) ,frame.size.height) collectionViewLayout:flowLayout];
        
        mCollection.dataSource = self;
        mCollection.delegate = self;
        [mCollection setBackgroundColor:[UIColor whiteColor]];
        mCollection.showsVerticalScrollIndicator=NO;
        mCollection.showsHorizontalScrollIndicator=NO;
        mCollection.scrollEnabled=NO;
        //注册Cell，必须要有
        [mCollection registerClass:[HomeNewClassCollectionCell class] forCellWithReuseIdentifier:@"cell"];

        [self addSubview:mCollection];

    }
    return self;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section==0) {
        return _dataSource.count<=2?_dataSource.count:2;
    }
    
    return _dataSource.count-2-(section-1)*3;
    //2  3  3
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (_dataSource.count<=2) {
        return 1;
    }
    return (_dataSource.count-2)%3==0?(_dataSource.count-2)/3+1:(_dataSource.count-2)/3+2;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeNewClassCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor=[UIColor whiteColor];
    
    if (indexPath.section==0)
    {
        cell.info=[_dataSource objectAtIndex:indexPath.row];
    }else
    {
        
        cell.info=[_dataSource objectAtIndex:2+(indexPath.section-1)*3+indexPath.row];
    }
    return cell;
}

#pragma mark --UICollectionViewDelegate
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return CGSizeMake(WFCGFloatX(170), WFCGFloatY(197));
    }
    return  CGSizeMake(WFCGFloatX(112), WFCGFloatY(141));
}
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GYNewShopTypeModel* model;
    if (indexPath.section==0)
    {
        model=[_dataSource objectAtIndex:indexPath.row];
    }else
    {
        
        model=[_dataSource objectAtIndex:2+(indexPath.section-1)*3+indexPath.row];
    }
    _backHandler(model.id);
}
@end


@implementation HomeNewClassCollectionCell
@synthesize imageIV,nameLB,info;
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
    
        imageIV=[[UIImageView alloc] init];


        imageIV.backgroundColor=[UIColor yellowColor];
        [self addSubview:imageIV];
        
        
        nameLB = [[UILabel alloc]init];
        [nameLB setTextColor:[UIColor redColor]];
        [nameLB setFont:SYSTEMFONT(15)];
        [nameLB setTextAlignment:NSTextAlignmentCenter];
        nameLB.backgroundColor=[UIColor clearColor];
        
        [self addSubview:nameLB];
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    
    imageIV.x=0;
    imageIV.width=self.width;
    imageIV.height=self.height;
    imageIV.y=0;
    
    [nameLB setFrame:CGRectMake(0, 0, self.width, 40)];
    
    
    [imageIV sd_setImageWithURL:[NSURL URLWithString: info.picurl] placeholderImage:DefaultImage];
  //  [nameLB setText:info.name];
}
@end
